/**
 * Project Name:BGI_DX
 * File Name:BaseDaoiBatis.java
 * Package Name:com.bgi.dx.common.pulgin
 * Date:2014-10-20下午09:39:42
 * Copyright (c) 2014, BGI_DX All Rights Reserved.
 *
 */
package com.bgi.uims.common.pulgin;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.inject.Inject;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import org.springframework.stereotype.Component;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapException;
import com.ibatis.sqlmap.engine.execution.SqlExecutor;
import com.ibatis.sqlmap.engine.impl.ExtendedSqlMapClient;
import com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate;
/**
 * ClassName:BaseDaoiBatis <br/>
 * Function: TODO ADD FUNCTION. <br/>
 * Reason:	 TODO ADD REASON. <br/>
 * Date:     2014-10-20 下午09:39:42 <br/>
 * @author   fukun
 * @version  
 * @since    JDK 1.7
 * @see 	 
 */
@SuppressWarnings("deprecation")
@Component
public abstract class BaseDaoiBatis extends SqlMapClientDaoSupport implements ICountRecords{

	/** ibatis执行sql类 */
	@Resource(name="sqlExecutor")
	private SqlExecutor sqlExecutor;

	@Inject
	@Resource(name="sqlMapClient")
	public void setSuperSqlMapClient(SqlMapClient sqlMapClient) {
		super.setSqlMapClient(sqlMapClient);
	}
	/**
	 * 把dialect注入到ibatis的sqlExecutor
	 * 
	 * @throws Exception
	 * @author lim
	 */
	@PostConstruct
	public void initialize() throws Exception {
		if (sqlExecutor != null) {
			SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
			if (sqlMapClient instanceof ExtendedSqlMapClient) {
				ReflectUtil.setFieldValue(((ExtendedSqlMapClient) sqlMapClient)
						.getDelegate(), "sqlExecutor", SqlExecutor.class,
						sqlExecutor);
			}
		}
	}

	public void setEnableLimit(boolean enableLimit) {
		if (sqlExecutor instanceof LimitSqlExecutor) {
			((LimitSqlExecutor) sqlExecutor).setEnableLimit(enableLimit);
		}
	}

	public long getObjectTotal(String selectQuery, Object parameterObject) {
		prepareCountQuery(selectQuery);
		return (Long) getSqlMapClientTemplate().queryForObject(
				CountStatementUtil.getCountStatementId(selectQuery),
				parameterObject);
	}

	public long getObjectTotal(String selectQuery) {
		prepareCountQuery(selectQuery);
		return (Long) getSqlMapClientTemplate().queryForObject(
				CountStatementUtil.getCountStatementId(selectQuery));
	}

	protected void prepareCountQuery(String selectQuery) {
		String countQuery = CountStatementUtil.getCountStatementId(selectQuery);
		if (logger.isDebugEnabled()) {
			logger.debug("Convert " + selectQuery + " to " + countQuery);
		}
		SqlMapClient sqlMapClient = getSqlMapClientTemplate().getSqlMapClient();
		if (sqlMapClient instanceof ExtendedSqlMapClient) {
			SqlMapExecutorDelegate delegate = ((ExtendedSqlMapClient) sqlMapClient)
					.getDelegate();
			try {
				delegate.getMappedStatement(countQuery);
			} catch (SqlMapException e) {
				delegate.addMappedStatement(CountStatementUtil
						.createCountStatement(delegate
								.getMappedStatement(selectQuery)));
			}

		}
	}
}
